package model

import (
	"github.com/jinzhu/gorm"
	uuid "github.com/satori/go.uuid"
	"time"
)

type Post struct {
	ID uuid.UUID `json:"id" gorm:"type:char(36);primary_key"` //char类型表示定长 varchar表示不定长
	UserId uint `json:"user_id" gorm:"not null"`
	CategoryId uint `json:"category_id" gorm:"not null"`  //如果 Category中有ID ，那么CategoryID就会自动为他的外键（grom规定
	Category *Category										//否则要手动 写 gorm:"foreignkey:xxxxx"
	Title string `json:"title" gorm:"type:varchar(50);not null"`
	HeadImg string `json:"head_img"` //图片的连接地址
	Content string `json:"content" gorm:"type:text;not null"`
	CreatedAt time.Time `json:"created_at" `
	UpdatedAt time.Time `json:"updated_at" `
}

func (p *Post)BeforeCreate(scope*gorm.Scope)error  {
	return scope.SetColumn("ID",uuid.NewV4())
}